package application;

import java.util.List;
import java.util.Properties;

import org.mybatis.guice.MyBatisModule;
import org.mybatis.guice.datasource.helper.JdbcHelper;

import persistence.BlogMapper;
import speed.context.SpeedApplicationModule;
import speed.mybatis.ProxoolPooledDriverManagerDataSourceProvider;

import com.google.inject.Binder;
import com.google.inject.Module;
import com.google.inject.name.Names;

import domain.Blog;

/**
 * 程序中module注册类
 * 
 * @author zhaohaolin(zhaohaolin2010@gmail.com)
 */
public class ApplicationModule implements SpeedApplicationModule {

	// 实现接口必要的方法，并在配置文件中配置好这个类就可以了
	public void registerModules(List<Module> modules) {
		modules.add(JdbcHelper.MySQL);
		modules.add(new Module() {
			public void configure(Binder binder) {
				Names.bindProperties(binder, createProperties());
			}
		});
		modules.add(new MyBatisModule.Builder()
				// .setDataSourceProviderType(PooledDataSourceProvider.class)
				// .setDataSourceProviderType(ProxoolPooledDataSourceProvider.class)
				.setDataSourceProviderType(
						ProxoolPooledDriverManagerDataSourceProvider.class)
				.addSimpleAliases(Blog.class)
				.addMapperClasses(BlogMapper.class).create());
	}

	/**
	 * @return
	 */
	Properties createProperties() {
		final Properties prop = new Properties();
		prop.setProperty("mybatis.environment.id", "test");
		prop.setProperty("proxool.alias", "proxool.test");
		return prop;
	}

}
